package com.thermal.thermal_inspection.service.impl;

import com.thermal.thermal_inspection.common.CommonResponse;
import com.thermal.thermal_inspection.entity.ClassUser;
import com.thermal.thermal_inspection.entity.Classes;
import com.thermal.thermal_inspection.entity.RealName;
import com.thermal.thermal_inspection.mapper.ClassMapper;
import com.thermal.thermal_inspection.mapper.ClassUserMapper;
import com.thermal.thermal_inspection.mapper.RealNameMapper;
import com.thermal.thermal_inspection.service.ClassService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;

@Service
public class ClassServiceImpl implements ClassService {
    @Autowired
    ClassMapper classMapper;
    @Autowired
    ClassUserMapper classUserMapper;
    @Autowired
    RealNameMapper realNameMapper;

    private static final Logger logger = Logger.getLogger(ClassServiceImpl.class);

    /**
     * 查看是否存在该班组
     */
    @Override
    public boolean existClass(int classId){
        Classes classes = classMapper.selectClass(classId);
        return classes != null;
    }

    /**
     *新增班组
     */
    @Override
    public int addClass(String className) {
        Timestamp now = new Timestamp(System.currentTimeMillis());
        return classMapper.addClass(className, now);
    }

    /**
     * 新增班组成员
     */
    @Override
    public int addUser(int classId, int userId){
        //先看是否存在该班组
        Classes classes = classMapper.selectClass(classId);
        if(classes == null) {
            return -1;
        }
        //再看班组中是否已存在该成员
        ClassUser classUser = classUserMapper.selectClassUser(classId, userId);
        if(classUser != null){
            return -2;
        }
        //修改real_name表中该成员的班组
        int rows1 = realNameMapper.changeClassId(userId, classId);
        //删除classes表中该成员
        int rows2 = classUserMapper.deleteUser(userId);
        return classUserMapper.addUser(classId, userId);
    }

    /**
     * 查看班组成员
     */
    @Override
    public List<RealName> getClassMembers(int classId){
        return realNameMapper.getClassMembers(classId);
    }

    /**
     * 删除班组
     */
    @Override
    public int deleteClass(int classId){
        int rows1 = classMapper.deleteClass(classId);
        int rows2 = classUserMapper.deleteClass(classId);
        int rows3 = realNameMapper.deleteClass(classId);
        if(rows1 != 0 && rows2 != 0 && rows3 != 0)
            return 1;
        return 0;
    }

    /**
     * 删除班组成员
     */
    @Override
    public int deleteUser(int classId, int userId){
        int rows1 = classUserMapper.deleteUserClass(classId, userId);
        int rows2 = realNameMapper.deleteUser(userId);
        if(rows1 != 0 && rows2 != 0)
            return 1;
        return 0;
    }

    /**
     * 查找所有班组
     */
    @Override
    public List<Classes> getClasses(){
        return classMapper.getClasses();
    }

    /**
     * 查询特定班组
     */
    @Override
    public Classes getOneClass(int classId){
        return classMapper.getOneClass(classId);
    }

    /**
     * 查询指定班组（搜索接口）
     */
    @Override
    public List<Classes> getSpecifiedClass(Integer classId, String className){
        return classMapper.getSpecifiedClass(classId, className);
    }

    /**
     * 判断班组名是否存在
     */
    @Override
    public boolean existClassName(String className){
        return classMapper.getClassByName(className) != null;
    }

}

